package com.xiantianxia.lty.plan.controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xiantianxia.lty.common.util.cache.UserCache;
import com.xiantianxia.lty.common.util.excel.handler.PlanImportVerifyHandler;
import com.xiantianxia.lty.plan.service.PlanService;
import com.xiantianxia.lty.plan.vo.ExcelImportPlanVo;
import com.xiantianxia.lty.plan.vo.PlanVo;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;

@Controller
@RequestMapping("/plan")
public class PlanController {

	@Resource
	private PlanService planService;

	@RequestMapping("/listView")
	public String listView(Model model) {
		List<PlanVo> page = page(null);
		model.addAttribute("page", page);
		return "/plan/list";
	}
	
	@RequestMapping("/modifyView/{planId}")
	public String modifyView(Model model, @PathVariable("planId")String planId) {
		PlanVo plan = planService.getById(planId);
		model.addAttribute("plan", plan);
		model.addAttribute("users", UserCache.userList);
		return "/plan/modify";
	}

	@RequestMapping("/page")
	@ResponseBody
	public List<PlanVo> page(PlanVo plan) {
		return planService.page(plan);
	}
	
	@RequestMapping("/modify")
	public String modify(PlanVo plan) {
		planService.modify(plan);
		return "redirect:/plan/listView";
	}

	@RequestMapping("/upload")
	public String upload(@RequestParam("file") MultipartFile file) throws Exception {
		if (file.isEmpty()) {
			return "redirect:/plan/listView";
		}
		String fileName = file.getOriginalFilename();
		int size = (int) file.getSize();
		System.out.println(fileName + "-->" + size);

		String path = "F:/test";
		File dest = new File(path + "/" + fileName);
		if (!dest.getParentFile().exists()) { // 判断文件父目录是否存在
			dest.getParentFile().mkdir();
		}
		file.transferTo(dest); // 保存文件

		ImportParams params = new ImportParams();
		params.setNeedVerfiy(true);
		params.setVerifyHandler(new PlanImportVerifyHandler());
		ExcelImportResult<ExcelImportPlanVo> result = ExcelImportUtil.importExcelMore(new FileInputStream(dest), ExcelImportPlanVo.class, params);
		
		List<ExcelImportPlanVo> list = result.getList();

		List<PlanVo> dbs = new ArrayList<>();
		for (ExcelImportPlanVo e : list) {
			PlanVo p = new PlanVo();
			p.setPlanDate(e.getPlanDate());
			p.setUserId(UserCache.getByUserName(e.getUserName().trim()).getUserId());
			p.setPlanName(e.getPlanName().trim());
			p.setCount(Integer.parseInt(e.getCount().trim()));
			dbs.add(p);
		}
		
		planService.batchInsert(dbs);
		
		return "redirect:/plan/listView";
	}
}
